package dao

import (
	"go_my_framework/app/core/db"
	"go_my_framework/app/model"
)

type RolePurviewDao interface {
	GetPurviewListByRoleId (int) ([]model.RolePurview, error)
	DelPurview(int, []int) error
	CreateMulti ([]model.RolePurview) error
}

func NewRolePurviewDaoImpl() RolePurviewDao {
	return new(RolePurviewDaoImpl)
}

type RolePurviewDaoImpl struct {

}

func (p *RolePurviewDaoImpl) GetPurviewListByRoleId (roleId int) (data []model.RolePurview, err error) {
	err = db.Instance().
		Model(model.RolePurview{}).
		Where("admin_role_id = ?", roleId).
		Find(&data).
		Error
	return data, err
}

func (p *RolePurviewDaoImpl) DelPurview(roleId int, purviewId []int) error {
	return db.Instance().
		Where("admin_role_id = ?", roleId).
		Where("admin_purview_id in ?", purviewId).
		Delete(model.RolePurview{}).
		Error
}

func (p *RolePurviewDaoImpl) CreateMulti (data []model.RolePurview) error {
	return db.Instance().Model(model.RolePurview{}).Create(data).Error
}